import scipy
from scipy import sparse
from svm import *
import os
import utils as utl

#Decommentex ces 3 imports pour les tests
#import learning as l
#from stemmer import PorterStemmer
#import testApp as app

class SVM(object):
    
    def __init__(self, matrix, clusters, words, matrix2):
        self.featuresMatrix = matrix
        self.clusters = clusters
        self.words = words
        self.matrix2 = matrix2
        self.retraceWords = matrix2.retraceWords
        self.positions = self.findClustersPositions()
        
        if os.access('../data/svm.ser', os.F_OK) :  
            self.model = svm_model('../data/svm.ser')
            self.maxFreq = utl.load('../data/svm1.ser')
        else:
            samples = matrix2.matrixFreq.toarray()
            labels = range(samples.__len__())
            
            maxFreq = 0
            for i in range(samples.__len__()):
                for j in range(samples[i].shape[0]):
                    if maxFreq < samples[i][j]:
                        maxFreq = samples[i][j]
            
            maxFreq = samples.max()
            
            print "MAxFreq : ", maxFreq
            
            samples = samples / maxFreq
            
            print "no instances : ", len(labels)
            print "no features : ", len(samples[0])
            
            problem = svm_problem(labels, samples)
            
            param = svm_parameter(C = 2.3, svm_type = 0 , kernel_type = LINEAR)
            print "Model..."
            model = svm_model(problem,param)
            print "Saving model..."
            model.save('../data/svm.ser')
            utl.save('../data/svm1.ser', maxFreq)
            
            print "Model saved !"
            self.model = model
            self.maxFreq = maxFreq
        
    def classify(self, ws):
        testdata = scipy.sparse.lil_matrix((1, len(self.words.bag)))
        for word, occurence in ws.iteritems():
            if word in self.words.bag:
                globalIndex = self.words.bag[word][0]
                if globalIndex in self.retraceWords:
                    realIndex = self.retraceWords[globalIndex]
                    testdata[0, realIndex] = occurence
        #print testdata.toarray()
        return self.clusters.cat2clus[self.model.predict(testdata.toarray()[0] / self.maxFreq)]
        #return self.model.predict(testdata.toarray()[0] / self.maxFreq)
        
        
    def findClustersPositions(self):
        pos = {}
        for i in range(self.featuresMatrix.shape[0]):
            if pos.has_key(self.featuresMatrix[i, 0]):
                pos[self.featuresMatrix[i, 0]].append(i)
            else:
                pos[self.featuresMatrix[i, 0]] = [i]
        
        return pos
       
''' Pour tester, ajouter les 3 imports commentes en haut 
s = SVM(app.reFeaturesMatrix, app.cls, app.allwords, app.matrix2)

p = PorterStemmer()
hits = 0

for i in os.listdir('../../Part1/awards_1994/awd_1994_00'):
    
    c, ws = l.getWords('../../Part1/awards_1994/awd_1994_00', i)

    candidate = s.predict(ws)
    
    if c in app.categories.bag.keys(): 
        catId = app.categories.bag[c][0]
        if catId in app.cls.positions[candidate]:
            hits += 1
print 'hits : ', hits
print 'succes : ', float(hits * 100) / os.listdir('../../Part1/awards_1994/awd_1994_00').__len__(), '%'
'''

